﻿using Awsc.DataAcquisitionSys.Core.Model.Config;
using Awsc.DataAcquisitionSys.Core.Models.ServiceModels;
using Awsc.DataAcquisitionSys.Core.Services.DataAcquisition.DataCollectors;
using Newtonsoft.Json.Linq;

namespace Awsc.DataAcquisitionSys.Core.Service.DataAcquisition.DataCollectors
{
    public class Awsc1000ACollector : ModbusTcpDataCollector
    {
        public Awsc1000ACollector(DataCollectionContext dataCollectionContext) : base(dataCollectionContext)
        {
        }
        /// <summary>
        /// 协议文档 PL10003协议：3开关量，9AI量，均使用ReadHoldingRegisters读取
        /// </summary>
        /// <returns></returns>
        protected async override Task CollectDataInternalAsync()
        {
            // 采集器输出的数据量，非传感器值，判断是否有AI
            if (CollectorConfig.AICount > 0)
            {
                ushort[] AIValue = ModbusMaster.ReadInputRegisters(CollectorConfig.SlaveAddress, 0, CollectorConfig.AICount);

                if (AIValue != null)
                {
                    for (int i = 0; i < AIValue.Length; i++)
                    {
                        SetAISensorValue(i + 1, AIValue[i]);
                    }
                }
            }
            //读取数字量输入
            if (CollectorConfig.DICount > 0)
            {
                bool[] ds = ModbusMaster.ReadInputs(CollectorConfig.SlaveAddress, 0, CollectorConfig.DICount);
                for (int i = 0; i < ds.Length && i < CollectorConfig.DICount; i++)
                {
                    SetDICoilValue(i + 1, ds[i]);
                }
            }
        }
    }
}
